package com.cris.search;

/**
 * Created by owen on 2017/3/30.
 */

/**
 * 基于无序链表的顺序查找
 * @param <Key>
 * @param <Value>
 */
public class SequentialSearchST <Key,Value> {

    private Node first;
    private class Node{
        Key key;
        Value val;
        Node next;
        public Node(Key key,Value val,Node next){
            this.key = key;
            this.val=val;
            this.next = next;
        }
    }

    public Value get(Key key) {
        //根据给定的键查找值
        for (Node x = first; x != null; x = x.next) {
            if (key.equals(x.key))
                return x.val;
        }
        return null;
    }

    /**
     *查找给定的键找到则更新，否则新建结点
     */
    public void put(Key key,Value val){
        for (Node x = first; x != null; x = x.next) {
            if (key.equals(x.key))
               x.val=val;
            return;
        }
        first = new Node(key,val,first);
    }


}
